package cn.wizzer.app.task.commons.ext.quartz.job;


import cn.wizzer.app.sys.modules.services.SysIPTablesService;
import cn.wizzer.app.sys.modules.services.SysTaskService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import java.util.Arrays;
import java.util.List;

/**
 * 定时处理访问ip
 */
@IocBean
public class IpTablesFilterJob implements Job {
    @Inject
    @Reference(retries = 3, timeout = 500000)
    private SysIPTablesService ipTablesService;

    @Inject
    @Reference
    private SysTaskService sysTaskService;

    private static final Log log = Logs.get();


    @Override
    public void execute(JobExecutionContext context) {
        JobDataMap data = context.getJobDetail().getJobDataMap();
        // 获取参数
        String[] split = data.getString("code").split("\\|");
        List<String> codes = Arrays.asList(split);
        // 第三方地址
        String website = data.getString("website");
        // 循环次数
        int forCount = data.getInt("forEach");
        ipTablesService.check(codes, forCount, website);
        // 记录执行时间
        String taskId = context.getJobDetail().getKey().getName();
        sysTaskService.update(Chain.make("exeAt", (int) (System.currentTimeMillis() / 1000))
                .add("exeResult", "执行成功"), Cnd.where("id", "=", taskId));
    }
}
